package com.leinao.nio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

/**
 *
 * @Description 文件或文件夹删除类
 * @author lizi
 * @date 2018/11/19 下午6:01
 * @Version 1.0
 */
public class FileDeleteTree extends SimpleFileVisitor<Path> {
    private static Logger logger = LoggerFactory.getLogger(FileDeleteTree.class);

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        logger.info("Delete file: {}", file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
        if (e == null) {
            Files.delete(dir);
            logger.info("Delete directory: {}", dir);
            return FileVisitResult.CONTINUE;
        } else {
            logger.error("postVisitDirectory IOException", e);
            throw e;
        }
    }
}
